//
//  DEMOSecondViewController.m
//  RESideMenuExample
//
//  Created by Roman Efimov on 10/10/13.
//  Copyright (c) 2013 Roman Efimov. All rights reserved.
//

#import "HQCommendViewController.h"
#import "HQViewController.h"
#import "HQApp.h"
#import "HQAppTableViewCell.h"
#import "UIView+Extension.h"
#import "UIButton+Extension.h"

@interface HQCommendViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *apps;

@end

@implementation HQCommendViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	self.title = @"推荐应用";
    
    //添加左边按钮
    UIButton *leftButton = [UIButton buttonWithImage:@"nav_button_back" title:@"返回" font:15 setColorRed:0.798 green:0.613 blue:0.392];
    [leftButton addTarget:self action:@selector(presentLeftMenuViewController:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];

    
    //添加右边导航按钮
    UIButton *rightButton = [UIButton buttonWithImage:@"nav_button" title:@"主页" font:15 setColorRed:0.798 green:0.613 blue:0.392];
    [rightButton addTarget:self action:@selector(pushViewController:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];

    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//    self.tableView.y = 64;
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.height -= 64;
    
}


//tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.apps.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HQAppTableViewCell *cell = [HQAppTableViewCell appCell:tableView];
//    cell.backgroundColor =  [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
    
    HQApp *app = self.apps[indexPath.row];
    
    cell.app = app;
    
    cell.contentView.transform = CGAffineTransformScale(cell.transform, 0.5, 0.5);
    [UIView animateWithDuration:0.5 animations:^{
        cell.contentView.transform = CGAffineTransformIdentity;
    }];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HQApp *app = self.apps[indexPath.row];
    UIApplication *application = [UIApplication sharedApplication];

    NSString *idString = [NSString stringWithFormat:@"%@",app.appId];
//    NSLog(@"%@",idString);
    
    NSString *urlString = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id%@?mt=8",idString];
    NSURL *url = [NSURL URLWithString:urlString];
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if ([application canOpenURL:url]) {
        [application openURL:url];
    }
}


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

}

- (void)pushViewController:(id)sender
{

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    HQViewController *viewController = [sb instantiateViewControllerWithIdentifier:@"main"];
//    viewController.title = @"收藏";
    [self.navigationController pushViewController:viewController animated:YES];

}

//懒加载

- (NSArray *)apps{
    if (_apps == nil) {
        _apps = [HQApp apps];
    }
    return _apps;
}


@end
